import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import jxl.write.WriteException;

import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException;


public class correct_json {
   
	
	public void correctJson(String fileName)
	{
		String str ="";
		try
		{
		BufferedReader reader = new BufferedReader(new FileReader(fileName));
		System.out.println("read was succesful");
		String line = null;
		while ((line=reader.readLine())!=null){
			str+=line;
		}
		reader.close();
		if(str.substring(0, 5).equals("{\"1\":"))
		{
			System.out.println("Already correct");
		}
		else{
		String newStr =  "{\"1\":"+str+"}";
		int start=2;
		System.out.println(newStr);
		// 43 +index of uuid
		int index=0;//,"2":
		while((newStr.length()- newStr.indexOf("uuid",index))>50)
		{
	    index=newStr.indexOf("uuid", index);
		int newIndex = index+43;
		
		String s1=newStr.substring(0, newIndex);
		String s2 = newStr.substring(newIndex, newStr.length());
		index=newIndex;
		newStr = s1+ ",\"" + start +"\":"+s2;
		System.out.println(" the value of start is :" + start); 
		//System.out.println(newStr);
		start++;
		}
		 BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));

		 writer.write(newStr);

		 writer.close();
		}
		}
		catch(Exception e)
		{
			System.out.println("error reading the file");
		}
		jsontoexcel je = new jsontoexcel();
		try {
			je.json_excel(fileName);
		} catch (JsonIOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonSyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
